package com.example.lottery.service.activitystatus.operator;

import com.example.lottery.dao.dataobject.ActivityPrizeDO;
import com.example.lottery.dao.dataobject.ActivityUserDO;
import com.example.lottery.dao.mapper.ActivityPrizeMapper;
import com.example.lottery.service.dto.ConvertActivityStatusDTO;
import com.example.lottery.service.enums.ActivityUserEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class PrizeOperator extends AbstractOperator{
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //1-    校验参数，目标状态是否为空 是否为COMPLETE
        if(convertActivityStatusDTO==null||
                convertActivityStatusDTO.getTargetActivityStatus()==null
                ||!convertActivityStatusDTO.getTargetActivityStatus().name().equalsIgnoreCase(ActivityUserEnum.COMPLETE.name())){
            log.warn("参数、目标状态错误");
            return false;
        }

        //2-    判断判断奖品、活动是否为空 后期进行扭转时会用到
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        if(activityId==null||prizeId==null){
            log.warn("奖品列表、活动为空");
            return false;
        }
        try {
            //3-    判断活动用户表中是否存在记录
            ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectPAInfo(prizeId, activityId);
            if(activityPrizeDO==null){
                log.warn("活动奖品表中不存在该条记录");
                return false;
            }
        }catch (Exception e){
            //sql异常
            log.warn("sql查询活动奖品表记录异常");
            return false;
        }
        return true;
    }

    @Override
    public Boolean convertStatus(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //1-    扭转
        try{
            int i=activityPrizeMapper.updateStatus(convertActivityStatusDTO.getPrizeId(),
                    convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetPrizeStatus().name());
            if(i==0){
                log.warn("更新活动奖品表失败");
                return false;
            }
        }catch (Exception e){
            log.warn("更新sql updateStatus error");
            return false;
        }
        return true;
    }
}
